/**
 * Mirrors an Erlang Chord node
 **/

package core1;

import java.util.List;

import processing.core.PApplet;

public class MirrorChordNode extends ChordNode implements Nodeable {
	public MirrorChordNode(PApplet parent, long key, String name) {
		super(parent, key);
		init();
	}
	public MirrorChordNode(PApplet parent, long key, String name, MirrorChordNode predNode, List<MirrorChordNode> fingerNodes) {
		super(parent, key);
		init();
		
		if (predNode != null)
			setPrevious(predNode);
		
		if (fingerNodes != null) {
			int index = 0;
			
			for (Long fingerKey : fingerTable.keySet()) {
				
				// TODO: This is backwards
				MirrorChordNode fingerNode = fingerNodes.get(index);
				index++;
				
				fingerTable.put(fingerKey, fingerNode);
			}
		}
	}
	private void init() {
		nextKey = getKey();
		previousKey = getKey();
		
		user = this;
		next = this;
		previous = this;
		
		//Create empty finger table
		
		for (Long key:fingerTable.keySet()) {
			fingerTable.put(key, this);
		}

	}
	
	public void updateNode(long key, String name, MirrorChordNode predNode, List<MirrorChordNode> fingerNodes) {
		if (predNode != null)
			setPrevious(predNode);
		
		if (fingerNodes != null) {
			int index = 0;
			
			for (Long fingerKey : fingerTable.keySet()) {
				
				// TODO: This is backwards
				MirrorChordNode fingerNode = fingerNodes.get(index);
				index++;
				
				fingerTable.put(fingerKey, fingerNode);
			}
		}
	}
	
	public void addElement(ChordElement e) {
		elements.add(e);

	}
	
	public void refreshElements() {
	
	}
}
